package com.sy.simpleegg.platform.kkb.network.errorhandler

import android.net.ParseException
import android.util.Log
import com.google.gson.JsonParseException
import org.apache.http.conn.ConnectTimeoutException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.net.ssl.SSLHandshakeException

/**
 * @author sy
 * @date 2024/10/11 15:08
 * @desc
 */
object ExceptionHandle {
    private val UNAUTHORIZED = 401
    private val FORBIDDEN = 403
    private val NOT_FOUND = 404
    private val REQUEST_TIMEOUT = 408
    private val INTERNAL_SERVER_ERROR = 500
    private val BAD_GATEWAY = 502
    private val SERVICE_UNAVAILABLE = 503
    private val GATEWAY_TIMEOUT = 504

    fun ExceptionHandle() {}

    fun handleException(e: Throwable?): ResponeThrowable? {
        var msg = ""
        val ex: ResponeThrowable
        return if (e is HttpException) {
            val httpException = e
            Log.i("HandleException", "httpException.code() : " + httpException.code())
            when (httpException.code()) {
                401, 403, 404, 408, 500, 502, 503, 504 -> {
                    msg = httpException.message.toString()
                    if (msg.isEmpty()) {
                        msg = "网络错误"
                    }
                    ex = ResponeThrowable(msg, 1003L)
                    ex
                }

                else -> {
                    msg = httpException.message.toString()
                    if (msg.isEmpty()) {
                        msg = "网络错误"
                    }
                    ex = ResponeThrowable(msg, 1003L)
                    ex
                }
            }
        } else if (e is ServerException) {
            val resultException = e
            msg = resultException.message
            ex = ResponeThrowable(msg, resultException.code)
            ex
        } else if (e !is JsonParseException && e !is JSONException && e !is ParseException) {
            if (e is ConnectException) {
                msg = "连接失败"
                ex = ResponeThrowable(msg, 1002L)
                ex
            } else if (e is SSLHandshakeException) {
                msg = "证书验证失败"
                ex = ResponeThrowable(msg, 1005L)
                ex
            } else if (e is ConnectTimeoutException) {
                msg = "连接超时"
                ex = ResponeThrowable(msg, 1006L)
                ex
            } else if (e is SocketTimeoutException) {
                msg = "连接超时"
                ex = ResponeThrowable(msg, 1006L)
                ex
            } else if (e is UnknownHostException) {
                msg = "无效的域名或没有网络连接"
                ex = ResponeThrowable(msg, 1007L)
                ex
            } else {
                msg = "未知错误"
                if (e != null) {
                    Log.e("HTTP", "" + e.message)
                    e.printStackTrace()
                }
                ex = ResponeThrowable(msg, 1000L)
                ex
            }
        } else {
            msg = "解析错误"
            ex = ResponeThrowable(msg, 1001L)
            ex
        }
    }


    class ServerException(override var message: String) : RuntimeException(message) {
        var code: Long = 0
    }


    class ResponeThrowable(override var message: String?, var code: Long) : Exception(
        message
    )


    object ERROR {
        const val UNKNOWN = 1000
        const val PARSE_ERROR = 1001
        const val NETWORD_ERROR = 1002
        const val HTTP_ERROR = 1003
        const val SSL_ERROR = 1005
        const val TIMEOUT_ERROR = 1006
        const val NET_ERROR = 1007
    }

}